﻿package com.cjh.lms.service;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;

import java.lang.management.LockInfo;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.security.auth.login.CredentialException;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cjh.lms.exception.LockEnterException;
import com.cjh.lms.mapper.TClientMapper;
import com.cjh.lms.mapper.TLockEnterMapper;
import com.cjh.lms.mapper.TLockInfoMapper;
import com.cjh.lms.mapper.TLockTypeMapper;
import com.cjh.lms.mapper.TReqMapper;
import com.cjh.lms.mapper.TUserMapper;
import com.cjh.lms.mapper.business.AllLockInfoMapper;
import com.cjh.lms.mapper.business.LockEnterMapper;
import com.cjh.lms.pojo.TLockEnter;
import com.cjh.lms.pojo.TLockEnterExample;
import com.cjh.lms.pojo.TLockInfo;
import com.cjh.lms.pojo.TLockInfoExample;
import com.cjh.lms.pojo.TLockType;
import com.cjh.lms.pojo.TLockTypeExample;
import com.cjh.lms.pojo.TUser;
import com.cjh.lms.pojo.TUserExample;
import com.cjh.lms.pojo.business.AllLockInfoModel;
import com.cjh.lms.pojo.business.LockEnterModel;
import com.sun.org.apache.bcel.internal.classfile.Code;

import javafx.scene.transform.Affine;


@Service("LockEnterService")
public class LockEnterService {
	
	@Autowired
	TLockTypeMapper tLockTypeMapper;
	@Autowired
	TLockEnterMapper tLockEnterMapper;
	@Autowired
	TLockInfoMapper tLockInfoMapper;
	@Autowired
	AllLockInfoMapper allLockInfoMapper;
	@Autowired
	LockEnterMapper lockEnterMapper;
	
	
	//锁具信息入库和产生入库记录
	@Transactional
	public Integer addLock(List<TLockInfo> tLockInfo,String tCode,String uCode){
		//生成入库记录
		Calendar calendar=Calendar.getInstance();
		Date eDate = calendar.getTime();
		int eNum =   tLockInfo.size();
		String eCode = UUID.randomUUID().toString().replace("-", "");
//		String uCode = tUser.getuCode();
		TLockEnter tLockEnter = new TLockEnter();
		tLockEnter.seteCode(eCode);
		tLockEnter.seteDate(eDate);
		tLockEnter.seteNum(eNum);
		tLockEnter.settCode(tCode);
		tLockEnter.setuCode(uCode);
		tLockEnterMapper.insertSelective(tLockEnter);
		//插入锁具信息
		for(int i = 0;i < eNum;i++){
			String lockNo = tLockInfo.get(i).getLockNo();
			TLockInfoExample tLockInfoExample = new TLockInfoExample();
			TLockInfoExample.Criteria criteria = tLockInfoExample.createCriteria();
			criteria.andLockNoEqualTo(lockNo);
			List<TLockInfo> a = tLockInfoMapper.selectByExample(tLockInfoExample);
			if(a.size()==0){
				tLockInfo.get(i).setlCode(UUID.randomUUID().toString().replace("-", ""));
				tLockInfo.get(i).seteCode(eCode);
				int check = tLockInfoMapper.insertSelective(tLockInfo.get(i)); 
				if(check != 1){
					LockEnterException e = new LockEnterException();
					e.setCheck(++i);
					throw e;
				}
			}else{
				LockEnterException lockEnterException = new LockEnterException();
				lockEnterException.setCount(++i);
				throw lockEnterException;
			}
		}
		System.err.println("LockEnterService addLock");
		return tLockInfo.size();
	}
	
	//锁具信息的删除
	public JSONObject deleteLockInfo(Integer tid){
		JSONObject jsonObject= new JSONObject();
		int a = tLockInfoMapper.deleteByPrimaryKey(tid);
		String data=null;
		if(a==1){
			data = "00";
			jsonObject.put("check",data);
		}else{
			data = "10001";
			jsonObject.put("check",data);
		}
		return jsonObject;
	}
	
	 //锁具信息详情查询
	public List<AllLockInfoModel> searchAllLockInfo(AllLockInfoModel allinfo){

		List<AllLockInfoModel> data = allLockInfoMapper.searchAllLockInfo(allinfo);
		return data;
	}
	
	//锁具信息的修改
	public String updataAllLockInfo(AllLockInfoModel allLockInfoModel){
		Integer tid = allLockInfoModel.getTid();
		String lockNo = allLockInfoModel.getLockNo();
		TLockInfo tLockInfo = tLockInfoMapper.selectByPrimaryKey(tid);
		System.out.println(tLockInfo.toString());
		tLockInfo.setLockNo(lockNo);
		int b = tLockInfoMapper.updateByPrimaryKeySelective(tLockInfo);
		String data = null;
		if(b==1){
			data = "00";
		}else{
			data = "10001";
		};
		return data;
	}
	
	//类型信息的展示
	public List<TLockType> selectLockType(TLockType tLockType){
		TLockTypeExample tLockTypeExample = new TLockTypeExample();
		List<TLockType> data = tLockTypeMapper.selectByExample(tLockTypeExample);
		return data;
	}
	
	//锁具类型新增
	@Transactional
	public JSONObject addLockType(TLockType tLockType){
		JSONObject jsonObject= new JSONObject();
		String locType = tLockType.getLocType();
		TLockTypeExample tLockTypeExample = new TLockTypeExample();
		TLockTypeExample.Criteria criteria = tLockTypeExample.createCriteria();
		criteria.andLocTypeEqualTo(locType);
		List<TLockType> a = tLockTypeMapper.selectByExample(tLockTypeExample);
		if(a.size()==0){
			tLockType.settCode(UUID.randomUUID().toString().replace("-", ""));
			int check = tLockTypeMapper.insert(tLockType);
			if(check != 1){
				LockEnterException e = new LockEnterException();
				e.setCheck(10001);
				throw e;
			}
		}else{
			LockEnterException e = new LockEnterException();
			e.setCheck(10002);
			throw e;
		}	
		jsonObject.put("check","00");
		return jsonObject;
	}
	
	//入库记录查询,可以根据入库人，入库时间。锁类型查询
	public List<LockEnterModel> searchLockEnter(LockEnterModel lockEnterModel){
		List<LockEnterModel> data = lockEnterMapper.getLockEnter(lockEnterModel);
		if(data != null){
			return data;
		}else{
			return null;	
		}
	} 
//	入库记录删除.
//	public String deleteLockEnter(String eCode){
//		TLockEnterExample tLockenterExample = new TLockEnterExample();
//		TLockEnterExample.Criteria criteria = tLockenterExample.createCriteria();
//		criteria.andECodeEqualTo(eCode);
//		tLockEnterMapper.deleteByExample(tLockenterExample);
//		JSONObject json = new JSONObject();
//		json.put("data", "删除成功");
//		String data = json.toJSONString();
//		return data;
//	}
}
